package com.hemaapp.yjnh.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.dialog.HemaButtonDialog;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseFragmentActivity;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.TabViewPagerAdapter;
import com.hemaapp.yjnh.fragment.CollectionListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;

/**
 * 我的收藏
 *
 * @author GuiyouZhang
 *         Date：2016年1月10日 下午3:59:52
 *         Email:guiyouzhang@163.com
 */
public class CollectionActivity extends BaseFragmentActivity implements OnClickListener {

    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.layout_tab)
    TabLayout layoutTab;
    @Bind(R.id.viewpager)
    ViewPager viewpager;

    private TabViewPagerAdapter pagerAdapter;
    List<Fragment> fragments = new ArrayList<>();
    boolean[] isEdits = new boolean[]{false, false, false, false, false, false, false, false, false};
    private CollectionListFragment liangkuFragment = new CollectionListFragment();
    private CollectionListFragment farmerFragment = new CollectionListFragment();
    private CollectionListFragment leleFragment = new CollectionListFragment();
    private CollectionListFragment zouzouFragment = new CollectionListFragment();
    private CollectionListFragment cunyouFragment = new CollectionListFragment();
    private CollectionListFragment OrangicFragment = new CollectionListFragment();
    private CollectionListFragment landmarkFragment = new CollectionListFragment();
    private CollectionListFragment techanFragment = new CollectionListFragment();
    private CollectionListFragment nongxuFragment = new CollectionListFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_collection);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        initDatas();
    }

    private void initDatas() {
        List<String> titles = new ArrayList<>();
        titles.add("粮库");
        titles.add("田家");
        titles.add("乐乐");
        titles.add("走走");
        titles.add("村游");
        titles.add("有机");
        titles.add("地标");
        titles.add("特产");
        titles.add("农需");
        fragments.add(liangkuFragment);
        fragments.add(farmerFragment);
        fragments.add(leleFragment);
        fragments.add(zouzouFragment);
        fragments.add(cunyouFragment);
        fragments.add(OrangicFragment);
        fragments.add(landmarkFragment);
        fragments.add(techanFragment);
        fragments.add(nongxuFragment);
        liangkuFragment.setKeytype("5");
        farmerFragment.setKeytype("22");
        leleFragment.setKeytype("6");
        zouzouFragment.setKeytype("7");
        cunyouFragment.setKeytype("8");
        OrangicFragment.setKeytype("2");
        landmarkFragment.setKeytype("1");
        techanFragment.setKeytype("9");
        nongxuFragment.setKeytype("10");

        pagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewpager.setAdapter(pagerAdapter);
        viewpager.setOffscreenPageLimit(9);
        layoutTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        layoutTab.setupWithViewPager(viewpager);
        layoutTab.setTabsFromPagerAdapter(pagerAdapter);

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (isEdits[position]) {
                    titleRightBtn.setText("完成");
                }else {
                    titleRightBtn.setText("编辑");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void getExras() {
    }

    @Override
    protected void setListener() {
        titleText.setText("我的收藏");
        titleRightBtn.setText("编辑");
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask hemaNetTask) {

    }

    @Override
    protected void callAfterDataBack(HemaNetTask hemaNetTask) {

    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {

    }

    @Override
    protected void callBackForServerFailed(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {

    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask hemaNetTask, int i) {

    }

    @OnClick({R.id.title_left_btn, R.id.title_right_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.title_right_btn:
                int index = viewpager.getCurrentItem();
                if (((CollectionListFragment)fragments.get(index)).getAdapter() != null) {
                    isEdits[index] = !isEdits[index];
                    ((CollectionListFragment) fragments.get(index)).setEdit(isEdits[index]);
                    if (isEdits[index]) {
                        titleRightBtn.setText("完成");
                    } else {
                        titleRightBtn.setText("编辑");
                    }
                }
                break;
        }
    }
}
